<?php
	if(isset($_POST['submit'])) {
	    require_once 'config/database.php';
	    $conn = mysqli_connect($db['hostname'],$db['username'],$db['password'],$db['database']) or die('Could not connect to DB');
	    $email = mysqli_real_escape_string($conn,$_POST['email']);
	    $passwd = mysqli_real_escape_string($conn,$_POST['password']);
	    $hash_pw = sha1($passwd);

	    $query =    "SELECT id, name
	                FROM members
	                WHERE email='$email'
	                AND passwd='$hash_pw'
	                LIMIT 1";
	    $result = mysqli_query($conn, $query) or die(mysqli_error($conn));
	    if(mysqli_num_rows($result) == 1) {
	        while($rows = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
	            $_SESSION['id'] = $rows['id'];
	            if (isset($_POST['remember'])) {
	            	setcookie("id", $_SESSION['id'], time()+ 7*24*60*60);
	            }
	            echo '<a href="logout.php" id="logoutButton" class="amenu">Logout</a>';
	        }
	    } else {
	    	// echo "<div id='loginfault'>sai toan khoan hoac mat khau!</div>";
	    	echo '<a href="#" id="loginButton" class="amenu"><span>Login</span></a>';
	    }
	} else {
		if(isset($_SESSION['id'])){
			echo '<a href="logout.php" id="logoutButton" class="amenu">Logout</a>';
		} else if (isset($_COOKIE['id'])) {
			$_SESSION['id'] = $_COOKIE['id'];
			echo '<a href="logout.php" id="logoutButton" class="amenu">Logout</a>';
		} else {
			echo '<a href="#" id="loginButton" class="amenu"><span>Login</span></a>';
		}
	}
?>